/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao.impl;

import dao.AgendouDAOException;
import dao.Conexao;
import dao.ServicosDAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import model.Servicos;

/**
 *
 * @author JeanLucas
 */
public class ServicosDAOImpl implements ServicosDAO {

    private Connection conn;

    public ServicosDAOImpl() throws AgendouDAOException {
        try {
            conn = Conexao.getConnection();
        } catch (Exception e) {
            throw new AgendouDAOException("Erro: \n" + e.getMessage());
        }
    }
    
    @Override
    public void atualizar(Servicos servico) throws AgendouDAOException {
        PreparedStatement ps = null;
        Connection conn1 = null;
        if (servico == null) {
            throw new AgendouDAOException("Nenhum cliente informado.");
        }
        try {
            String sql = "UPDATE agendou.servicos SET nome_servico=?, is_ativo=? WHERE nome_servico=? ";
            conn1 = conn;
            ps = conn1.prepareStatement(sql);
            ps.setString(1, servico.getNomeServico());
            ps.setBoolean(2, servico.getAtivo());
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new AgendouDAOException("Erro ao atualizar o usuário. \n" + e);
        } finally {
            Conexao.closeConnection(conn1, ps);
        }
    }

    @Override
    public void excluir(Servicos servico) throws AgendouDAOException {
        PreparedStatement ps = null;
        Connection conn1 = null;
        if (servico == null) {
            throw new AgendouDAOException("Nenhum usuário informado.");
        }
        try {
            String sql = "UPDATE agendou.servicos SET is_ativo = false WHERE nome_servico =?";
            conn1 = conn;
            ps = conn1.prepareStatement(sql);
            ps.setString(1, servico.getNomeServico());
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new AgendouDAOException("Erro ao excluir o serviço. \n" + e);
        } finally {
            Conexao.closeConnection(conn1, ps);
        }
    }

    @Override
    public void salvar(Servicos servico) throws AgendouDAOException {
        PreparedStatement ps = null;
        Connection conn1 = null;
        if (servico == null) {
            throw new AgendouDAOException("Nenhum cliente informado.");
        }
        try {
            String sql = "INSERT INTO agendou.servicos (nome_servico, is_ativo) values (?,?) ";
            conn1 = conn;
            ps = conn1.prepareStatement(sql);
            ps.setString(1, servico.getNomeServico());
            ps.setBoolean(2, servico.getAtivo());
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new AgendouDAOException("Erro ao atualizar o usuário. \n" + e);
        } finally {
            Conexao.closeConnection(conn1, ps);
        }
    }

    @Override
    public List<Servicos> todosServicos() throws AgendouDAOException {
        PreparedStatement ps = null;
        Connection conn1 = null;
        ResultSet rs = null;
        try {
            String sql = "SELECT * FROM agendou.servicos ";
            conn1 = conn;
            ps = conn1.prepareStatement(sql);
            rs = ps.executeQuery();
            List<Servicos> servicos = new ArrayList<Servicos>();
            while (rs.next()) {
                Servicos servico = new Servicos(rs.getInt("idservicos"), rs.getString("nome_servico"), rs.getBoolean("is_ativo"));
                servicos.add(servico);
            }
            return servicos;
        } catch (SQLException e) {
            throw new AgendouDAOException("Erro ao procurar cliente. \n" + e);
        } finally {
            Conexao.closeConnection(conn1, ps, rs);
        }
    }
    
    @Override
    public Servicos getServico(Servicos servico) throws AgendouDAOException{
        PreparedStatement ps = null;
        Connection conn1 = null;
        ResultSet rs = null;
        
        try {
            String sql1 = "SELECT * FROM agendou.servicos WHERE nome_servico =?";
            conn1 = this.conn;
            ps = conn1.prepareStatement(sql1);
            ps.setString(1, servico.getNomeServico());
            rs = ps.executeQuery();
            
            Servicos resultado;
            if( !rs.next()) {
                resultado = new Servicos();
            } else {
                resultado = new Servicos(rs.getInt("idservicos"), rs.getString("nome_servico"), rs.getBoolean("is_ativo"));
            }
            return resultado;
            
        } catch (SQLException e){
            throw new AgendouDAOException("Erro ao procurar horário " + e);
        } finally {
            Conexao.closeConnection(conn1, ps, rs);
        }
    }
    
    @Override
    public Servicos getServico(int idservico) throws AgendouDAOException {
        PreparedStatement ps = null;
        Connection conn1 = null;
        ResultSet rs = null;
        Servicos usuario = new Servicos();
        if (idservico == 0) {
            throw new AgendouDAOException("Nenhum ID de serviço informado.");
        }
        try {
            String sql = "SELECT * FROM agendou.servicos WHERE idservicos=?";
            conn1 = conn;
            ps = conn1.prepareStatement(sql);
            ps.setInt(1, idservico);
            rs = ps.executeQuery();
            Servicos resultado;
            if( !rs.next()) {
                resultado = new Servicos();
            } else {
                resultado = new Servicos(rs.getInt("idservicos"), rs.getString("nome_servico"), rs.getBoolean("is_ativo"));
            }
            return resultado;
        } catch (SQLException e) {
            throw new AgendouDAOException("Erro ao procurar cliente. \n" + e);
        } finally {
            Conexao.closeConnection(conn1, ps, rs);
        }
    }
    
}
